package com.by.controller;

import com.alibaba.fastjson.JSON;
import com.by.domain.Product;
import com.by.impl.ProductInterfaceImpl;
import com.by.mapper.ProductInterface;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;

/**
 * <p><p/>
 *
 * @author boyin
 * @ClassName ProductController
 * @Description 商品访问类
 * @date 2024/12/24 22:29
 */
@WebServlet("/product/searchProducts")
public class ProductSearchController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        ProductInterface productInterface = new ProductInterfaceImpl();
        try {
            List<Product> productList = productInterface.searchAllProduct();
            String jsonString = JSON.toJSONString(productList);
            out.write(jsonString);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
